package com.aliang.shopping.controller;

import cn.hutool.core.util.ObjectUtil;
import com.aliang.shopping.common.PageResult;
import com.aliang.shopping.common.Result;
import com.aliang.shopping.common.ResultCodeEnum;
import com.aliang.shopping.contact.ResultMsgContact;
import com.aliang.shopping.exception.DataException;
import com.aliang.shopping.log.annotation.OperateLog;
import com.aliang.shopping.log.enums.OperateType;
import com.aliang.shopping.model.dto.common.PageDTO;
import com.aliang.shopping.model.vo.loginLog.LoginLogExportVO;
import com.aliang.shopping.model.vo.loginLog.LoginLogVO;
import com.aliang.shopping.security.PreventRepeatSubmit;
import com.aliang.shopping.service.LoginLogService;
import com.aliang.shopping.util.excel.ExcelUtils;
import com.alibaba.excel.EasyExcel;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/login_log")
@AllArgsConstructor
public class LoginLogController {

    private final LoginLogService loginLogService;
    private final ExcelUtils excelUtils;

    /**
     * 登录日志删除
     *
     * @param ids
     * @return
     */
    @DeleteMapping("/del")
    @ApiOperation("登录日志删除")
    @PreventRepeatSubmit
    @OperateLog(title = "登录日志删除", type = OperateType.DELETE)
    public Result<String> delLoginLog(@RequestBody List<Long> ids) {
        this.loginLogService.removeByIds(ids);
        return Result.ok(ResultMsgContact.DELETE_SUCCESS);
    }


    /**
     * 登录日志分页搜索
     */
    @GetMapping("/getPage")
    @ApiOperation("登录日志分页搜索")
    public Result<PageResult<LoginLogVO>> queryLoginLogPage(PageDTO pageDTO) {
        //必要字段校验
        if (ObjectUtil.hasNull(pageDTO.getPageSize(), pageDTO.getPageNum())) {
            throw new DataException(ResultCodeEnum.DATA_LACK);
        }
        //查询数据
        PageResult<LoginLogVO> result = this.loginLogService.selectPage(pageDTO);
        return Result.ok(result);
    }

    /**
     * 登录日志导出
     */
    @ApiOperation("导出登录日志信息")
    @GetMapping("/export")
    @PreventRepeatSubmit
    @OperateLog(title = "导出登录日志信息", type = OperateType.EXPORT)
    public void exportLoginLog(PageDTO pageDTO, HttpServletResponse response) throws IOException {
        String fileName = "登录日志列表.xlsx";
        // 设置请求头
        this.excelUtils.setHeader(response, fileName);
        List<LoginLogExportVO> exportUserList = this.loginLogService.getExportList(pageDTO);
        //查询数据
        EasyExcel.write(response.getOutputStream(), LoginLogExportVO.class).sheet("登录日志列表")
                .doWrite(exportUserList);
    }

    /**
     * 根据关键词查询用户登录日志
     */
    @GetMapping("/getLoginLogByKeyword")
    @ApiOperation("根据关键词查询用户登录日志")
    public Result<List<LoginLogVO>> getLoginLogByKeyword() {
        List<LoginLogVO> loginLogByKeyword = this.loginLogService.getLoginLogByKeyword();
        return Result.ok(loginLogByKeyword);
    }
}
